package org.testis.data;

import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import java.util.LinkedList;
import javax.jms.JMSException;
import javax.jms.Message;
import org.springframework.jms.JmsException;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessagePostProcessor;

/**
 * Defines a singleton class to send  notification to abort all test steps 
 * @author nurhat
 */
public class TestStepAbortAll {

    private static TestStepAbortAll m_sender;
    private JmsTemplate m_jmsTemplate;

    private TestStepAbortAll() {
    }

    /**
     * A static method for creating singleton instance. This method
     * used in  spring framework supported configuration files
     * to create an instance of this class to be used globally.
     * @param m_jmsTemplate for handling message destination  property.
     * Initially it is configured in  spring framework supported configuration files
     * @return
     */
    public static TestStepAbortAll createInstance(
            JmsTemplate jmsTemplate) {
        if (m_sender == null) {
            m_sender = new TestStepAbortAll();
            m_sender.m_jmsTemplate = jmsTemplate;
        }

        return m_sender;
    }

    public static TestStepAbortAll getInstance() {
        return m_sender;
    }

    /**
     * Sends a  TestStepDataObject over jms using setting configured spring framework supported
     * configuration files
     * @param data
     */
    public synchronized boolean sendData(final LinkedList<String> data) {


        try {
            m_jmsTemplate.convertAndSend(data,
                    new MessagePostProcessor() {

                        public Message postProcessMessage(
                                Message message)
                                throws JMSException {

                            return message;
                        }
                    });
            return true;
        } catch (JmsException ex) {
        }
        return false;
    }
}
